﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace HospitalSystem
{
    public partial class ReportSummation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadChart(this.SortingTable(), "fundata");
            }
        }

        //LoadChart For GetJson To Show Graph(using Google Visualization)
        private void LoadChart(DataTable table, string jsName)
        {
            var gdt = new Bortosky.Google.Visualization.GoogleDataTable(table);
            using (var mem = new System.IO.MemoryStream())
            {
                gdt.WriteJson(mem);
                mem.Position = 0;
                using (var sr = new System.IO.StreamReader(mem))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), jsName, string.Format("var {0} = {1};", jsName, sr.ReadToEnd()), true);
                    sr.Close();
                };
            }
        }

        protected DataTable SortingTable()
        {
            DataTable BindGridview = new DataTable();
            BindGridview.Columns.Add("กิจกรรมหลัก");
            BindGridview.Columns.Add("จำนวนคน");
            BindGridview.Columns.Add("ค่าวัสดุ/คน", typeof(System.Double));
            BindGridview.Columns.Add("ค่าเสื่อมครุภัณฑ์/คน", typeof(System.Double));
            BindGridview.Columns.Add("ค่าเสื่อมอาคาร/คน", typeof(System.Double));
            BindGridview.Columns.Add("ค่าตอบแทนบุคลากร/คน", typeof(System.Double));
            BindGridview.Columns.Add("ค่าใช้จ่ายอื่นๆ/คน", typeof(System.Double));
            BindGridview.Columns.Add("ค่าใช้จ่ายรวม/คน", typeof(System.Double));
            BindGridview.Columns.Add("ค่าใช้จ่ายรวมทั้งหมด", typeof(System.Double));

            List<string> ActivityList = new List<string>();
            List<string> NameActivityList = new List<string>();
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            scon.Open();
            SqlCommand cmd1 = new SqlCommand("select Activity.ACTCode,Activity.ACTDesc from Activity join ActivityCost on Activity.ACTCode = ActivityCost.ACTCode", scon);
            SqlDataReader reader1 = cmd1.ExecuteReader();
            while (reader1.Read() != false)
            {
                ActivityList.Add(reader1["ACTCode"].ToString());
                NameActivityList.Add(reader1["ACTDesc"].ToString());
            }
            reader1.Close();

            for (int i = 0; i < ActivityList.Count; i++)
            {
                string activityCode = ActivityList[i];
                string CountHuman = "";
                string LabourCost = "";
                string MatherialCost = "";
                string CC_Equipment = "";
                string CC_Building = "";
                string IndirectCost = "";
                string Others = "";
                string CurrentCost = "";
                double Totalmoney = 0.0;
                SqlCommand cmd2 = new SqlCommand("select COUNT(DISTINCT ServiceProvisionVsPerson.CID)as Counter from ServiceProvisionDetails inner join ServiceProvisionVsPerson on ServiceProvisionDetails.PSNumber = ServiceProvisionVsPerson.PSNumber where ServiceProvisionDetails.ACTCode = @ACT", scon);
                cmd2.Parameters.AddWithValue("@ACT", activityCode);
                SqlDataReader reader2 = cmd2.ExecuteReader();
                while (reader2.Read() != false)
                {
                    CountHuman = reader2["Counter"].ToString();
                }
                reader2.Close();

                SqlCommand cmd3 = new SqlCommand("select * from ActivityCost where ACTCode = @ACT", scon);
                cmd3.Parameters.AddWithValue("@ACT", activityCode);
                SqlDataReader reader3 = cmd3.ExecuteReader();
                while (reader3.Read() != false)
                {
                    LabourCost = reader3["LabourCost"].ToString();
                    MatherialCost = reader3["MatherialCost"].ToString();
                    CC_Equipment = reader3["CC_Equipment"].ToString();
                    CC_Building = reader3["CC_Building"].ToString();
                    IndirectCost = reader3["IndirectCost"].ToString();
                    Others = reader3["Others"].ToString();
                    CurrentCost = reader3["CurrentCost"].ToString();
                }
                Totalmoney = Convert.ToDouble(CountHuman) * Convert.ToDouble(CurrentCost);
                //BindGridview.Rows.Add(NameActivityList[i], CountHuman, ProposedCost, AveragePerChild);
                BindGridview.Rows.Add(NameActivityList[i], CountHuman, MatherialCost, CC_Equipment, CC_Building, LabourCost, IndirectCost, CurrentCost, Totalmoney);
                reader3.Close();

            }
            scon.Close();
            return BindGridview;
        }
    }
}